use anyhow::Result;
use crm_metadata::{AppConfig, MetadataService};
use tonic::transport::Server;
use tracing::{info, level_filters::LevelFilter};
use tracing_subscriber::{fmt::Layer, layer::SubscriberExt, util::SubscriberInitExt, Layer as _};

#[tokio::main]
async fn main() -> Result<()> {
    let layer = Layer::new().with_filter(LevelFilter::INFO);
    tracing_subscriber::registry().with(layer).init();
    let config = AppConfig::load().expect("load config failed");
    let addr = format!("[::1]:{}", config.server.port)
        .parse()
        .expect("parse addr failed");
    info!("listening on {}", addr);
    let service = MetadataService::new(config);
    Server::builder()
        .add_service(service.into_server())
        .serve(addr)
        .await
        .expect("run server failed");

    Ok(())
}
